import {
  list
} from '../../../api/order';
import {
  list as refundList
} from '../../../api/askfor';
const util = require('../../../utils/util.js')
let app = getApp();
Page({
  data: {
    currentTab: 0,
    pageNum: 1,
    pageSize: 5,
    orderList: [],
    paymentStatus: 0, // 付款状态
    shippingStatus: 0, // 配送状态
    orderStatus: 0, // 订单状态
    loading: true,
    arr: [],
    triggered: false,
    refreshType: false,
    refundForm:[]
  },
  onLoad: function (query) {
    var that = this;
    /**
     * 获取当前设备的宽高
     */
    this.setNavSize()
    this.setData({
      paymentStatus: '', // 付款状态
      shippingStatus: '', // 配送状态
      orderStatus: '', // 订单状态
      currentTab: query.index,
    })
    console.log(query)
    this.data.refreshType = true
    if (query.index == 0) {
      this.bindChange()
    }
  },
  onShow: function () {
    if (this.data.refreshType == false) {
      this.bindChange()
      console.log('111')
    }
    this.data.refreshType = false
  },
  //  tab切换逻辑
  swichNav: function (e) {
    var _this = this;
    if (e !== undefined) {
      if (this.data.currentTab === e.target.dataset.current) {
        return false;
      } else {
        _this.data.currentTab = e.target.dataset.current
        _this.setData({
          currentTab: e.target.dataset.current
        })
      }
    }
  },

  bindChange: function (e) {
    var _this = this;
    console.log(e)
    if (!util.isNull(e)) {
      _this.data.currentTab = e.detail.current
    }
    this.data.pageNum = 1;
    this.data.orderList = [];
    this.data.refundForm = []
    var paymentStatus = ''; // 付款状态
    var shippingStatus = ''; // 配送状态
    var orderStatus = ''
    if(_this.data.currentTab != 4){
      switch (_this.data.currentTab) {
        case 0:
          paymentStatus = ''; // 付款状态
          shippingStatus = ''; // 配送状态
          orderStatus = '' // 订单状态
          break;
        case 1:
          paymentStatus = 0; // 付款状态
          shippingStatus = 0; // 配送状态
          orderStatus = 0 // 订单状态
          break;
        case 2:
          paymentStatus = 1; // 付款状态
          shippingStatus = 1; // 配送状态
          orderStatus = 1 // 订单状态
          break;
        case 3:
          paymentStatus = 1; // 付款状态
          shippingStatus = 1; // 配送状态
          orderStatus = 2 // 订单状态
          break;
      }
      _this.setData({
        paymentStatus: paymentStatus,
        shippingStatus: shippingStatus,
        orderStatus: orderStatus,
        currentTab: _this.data.currentTab
      })
      this.loadList()
    }else{
      this.loadrefund()
    }

  },
  loadList() {
    var _this = this
    this.data.loading = false
    wx.showLoading({
      title: '加载中',
      mask:true
    })
    list({
      pageNum: _this.data.pageNum,
      pageSize: _this.data.pageSize,
      paymentStatus: _this.data.paymentStatus, // 付款状态
      shippingStatus: _this.data.shippingStatus, // 配送状态
      orderStatus: _this.data.orderStatus, // 订单状态
      mchId: app.globalData.mchId
    }).then(
      res => {
        console.log(res)
        if (res.type == 'success') {
          res.data.data.forEach(function (item) {
            item.itemList.forEach(function (il) {
              il.thumbnail = util.thumbnail(il.thumbnail, 170, 170)
            })
            item.createDate = util.datetimeFormat(item.createDate)
            _this.data.orderList.push(item)
          })
          console.log(_this.data.orderList)
          _this.data.loading = _this.data.pageSize <= res.data.data.length;
          wx.hideLoading()
          _this.setData({
            orderList: _this.data.orderList,
          })
        } else {
          wx.hideLoading()
        }
      }
    ).catch(error => {
      console.log(error)
      this.data.loading = true
      wx.hideLoading()
    })
  },
  reachBottom() {
    var _this = this
    if (_this.data.loading == true) {
      this.data.pageNum = this.data.pageNum + 1;
      if(_this.data.currentTab !=4){
        _this.loadList()
      }else{
        _this.loadrefund()
      }

    }
  },
  setNavSize: function () {
    var that = this,
      sysinfo = wx.getSystemInfoSync(),
      statusHeight = sysinfo.statusBarHeight,
      isiOS = sysinfo.system.indexOf('iOS') > -1,
      navHeight;
    console.log(sysinfo)
    if (!isiOS) {
      navHeight = 48;
    } else {
      navHeight = 44;
    }
    that.setData({
      status: statusHeight,
      navHeight: navHeight
    })
  },
  navTo() {
    let p = getCurrentPages().length;
    if (p <= 1) {
      wx.switchTab({
        url: '/pages/index/index',
      })
    } else {
      wx.navigateBack({
        delta: 1
      })
    }
  },
  refresh() {
    this.bindChange()
  },
  // 退款
  loadrefund() {
    var _this = this
    _this.data.loading = false
    wx.showLoading({
      title: '加载中',
      mask:true
    })
    refundList({
      pageNum: _this.data.pageNum,
      pageSize: _this.data.pageSize,
      mchId: app.globalData.mchId
    }).then(
      res => {
        console.log(res)
        wx.hideLoading()
        if (res.type == 'success') {
          res.data.data.forEach(function (item) {
            item.itemList.forEach(function (il) {
              il.thumbnail = util.thumbnail(il.thumbnail, 170, 170)
            })
            item.createDate = util.datetimeFormat(item.createDate)
            _this.data.refundForm.push(item)
          })
          _this.data.loading = _this.data.pageSize <= res.data.data.length;
          _this.setData({
            refundForm: _this.data.refundForm,
          })
        }
      }
    ).catch(error => {
      wx.hideLoading()
      console.log(error)
    })
  }
})
